/*******************************************************************************
 * @project: CDMS
 * @package: com.esoon.cdms.util
 * @file: CapitalWebServiceUtil.java
 * @author: zhangpei
 * @created: 2017-2-13
 * @purpose:
 * 
 * @version: 1.0
 * 
 * Revision History at the end of file.
 * 
 * Copyright 2017 AcconSys All rights reserved.
 ******************************************************************************/

/**
 * 
 */
package com.esoon.cdms.util;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.soap.SOAPException;

import org.xml.sax.SAXException;

import com.acconsys.cws.CisServer;
import com.acconsys.cws.SOAPClient;
import com.acconsys.cws.SOAPRequest;
import com.acconsys.cws.SOAPResponse;

/**
 * @author Administrator
 * 
 */
public class CapitalWebServiceUtil {

	public static void main(String[] args) throws Exception {
		getAllProjectXml();
	}

	public static File getAllProjectXml() throws Exception {

		CisServer server = new CisServer("127.0.0.1", 49901);
		SOAPRequest request = new SOAPRequest();
		request.setUsername("system");
		request.setPassword("manager");
		request.setPayload("<projects/>");
		request.build(true);
		SOAPClient client = new SOAPClient();
		SOAPResponse response = client.callService(request,
				server.getProjectsListService());

		File file = new File("E:/workspace_capital/CDMS/temp/Projects.xml");
		response.saveBody(file);
		System.out.println(file.getAbsolutePath());
		File returnFile = new File(file.getAbsolutePath());
		System.out.println(returnFile.getAbsolutePath());
		// if(!returnFile.exists()){
		// System.out.println(returnFile+"文件不存在！");
		// throw new FileNotFoundException();
		// }

		return returnFile;

	}

	public static File getAllDesignByProjectName(String projectName)
			throws Exception {

		CisServer server = new CisServer("localhost", 49901);// 这里本机测试时，local等价于127.0.0.1
		SOAPRequest request = new SOAPRequest();
		request.setUsername("system");
		request.setPassword("manager");

		request.setPayload("<project name='" + projectName + "'/>");
		request.build(true);
		SOAPClient client = new SOAPClient();
		SOAPResponse response = client.callService(request,
				server.getDesignsListService());

		File file = new File("E:/workspace_capital/CDMS/temp/Designslist_"
				+ projectName + ".xml");
		response.saveBody(file);
		System.out.println(file.getAbsolutePath());
		File returnFile = new File(file.getAbsolutePath());
		System.out.println(returnFile.getAbsolutePath());
		// if(!returnFile.exists()){
		// System.out.println(returnFile+"文件不存在！");
		// throw new FileNotFoundException();
		// }

		return returnFile;

	}

	public static File getDesignXmlByDesignId(String designId)
			throws Exception {

		CisServer service = new CisServer("localhost", 49901);
		SOAPRequest request = new SOAPRequest();
		request.setUsername("system");
		request.setPassword("manager");
		// request.setPayload("<wiringdesign name='Design1' revision='G'  projectname='TA600'/>");
		request.setPayload("<wiringdesign id='"+designId+"'/>");
		request.build(true);
		SOAPClient client = new SOAPClient();
		SOAPResponse response = client.callService(request,
				service.getDesignExporterService());
		// response.addDocumentType();

		File file = new File("E:/workspace_capital/CDMS/temp/DesignExporter_"
				+ designId + ".xml");
		response.saveBody(file);
		System.out.println(file.getAbsolutePath());
		File returnFile = new File(file.getAbsolutePath());
		System.out.println(returnFile.getAbsolutePath());

		return returnFile;

	}
}

/*******************************************************************************
 * <B>Revision History</B><BR>
 * [type 'revision' and press Alt + / to insert revision block]<BR>
 * 
 * 
 * 
 * Copyright 2017 AcconSys All rights reserved.
 ******************************************************************************/
